/*************************************************************************
	> File Name: 9.solve-2.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2024-11-25 14:17:33
 ************************************************************************/

#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX_T 1000
#define MAX_M 100

int dp[MAX_T + 5];
int w[MAX_M + 5];
int val[MAX_M + 5];

int main() {
    int t, m;
    cin >> t >> m;
    for (int i = 1; i <= m; i++) {
        cin >> w[i] >> val[i];
    }
    for (int i = 1; i <= m; i++) {
        for (int j = t; j >= w[i]; j--) {
            dp[j] = max(dp[j], dp[j - w[i]] + val[i]);
        }
    }
    cout << dp[t] << endl;
    return 0;
}
